package com.tajiang.business.setting.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.constant.TJCst;
import com.tajiang.business.common.fragment.BaseFragment;
import com.tajiang.business.common.http.BaseResponse;
import com.tajiang.business.common.http.HttpHandler;
import com.tajiang.business.common.http.HttpResponseListener;
import com.tajiang.business.common.utils.SharedPreferencesUtils;
import com.tajiang.business.common.utils.ToastUtils;
import com.tajiang.business.common.utils.UserUtils;
import com.tajiang.business.common.widget.LoadingDialog;
import com.tajiang.business.common.widget.SwitchView;
import com.tajiang.business.login.activity.LoginActivity;
import com.tajiang.business.model.Bank;
import com.tajiang.business.model.User;
import com.tajiang.business.setting.activity.PaySettingActivity;
import com.tajiang.business.setting.activity.PrintsettingActivity;
import com.tajiang.business.setting.dialog.SetAudioDialog;
import com.tajiang.business.setting.dialog.SetBusinessStateDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//import com.leifeng.ceo.common.activity.

/**
 * 设置
 * Created by Administrator on 2016/5/10.
 */
public class SettingFragment extends BaseFragment {

    @BindView(R.id.rl_setting_admin)
    RelativeLayout rlSettingAdmin;
    @BindView(R.id.rl_setting_pay)
    RelativeLayout rlSettingPay;
    @BindView(R.id.btn_quit)
    Button btnQuit;
    @BindView(R.id.rl_printing_setting)
    RelativeLayout rlPrintingSetting;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.sv_business_state)
    SwitchView svBusinessState;
    @BindView(R.id.sv_audio_button)
    SwitchView svAudioButton;

    private LoadingDialog loadingDialog;

    @Override
    protected void initTopBar() {
        setTitle(getResourcesString(R.string.msg_setting));
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_setting);
        this.svAudioButton.setSwitchButtonMode(SwitchView.MODE_SINGLE_CLICK);
//        this.tvAppVersion.setText(TJCst.IS_DEBUG ? "测试版" : "");
    }

    @Override
    protected void initData() {
        loadingDialog = new LoadingDialog(getActivity());
//        tvUserPhone.setText(UserUtils.getUser().getPhone());
//        svBusinessState.setOpened(UserUtils.getUser().getState() == User.STATE_OPEN);
        svAudioButton.setOpened((Boolean) SharedPreferencesUtils.get(SharedPreferencesUtils.AUDIO_ATTENTION, false));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.rl_setting_pay)
    public void onSettingPayClick() {
        if ((System.currentTimeMillis() - currentTime) < TIME_IN_MILLS) return;
        currentTime = System.currentTimeMillis();
        checkBank();
    }

    @OnClick(R.id.btn_quit)
    public void onQuitClick() {
        SharedPreferencesUtils.remove(SharedPreferencesUtils.USER_LOGIN_INFOR);
        SettingFragment.this.getActivity().finish();
        intent2Activity(LoginActivity.class);
    }

    @OnClick(R.id.rl_printing_setting)
    public void onPrintingSettingClick() {
        if ((System.currentTimeMillis() - currentTime) < TIME_IN_MILLS) return;
        currentTime = System.currentTimeMillis();
        intent2Activity(PrintsettingActivity.class);
    }

//    @OnClick(R.id.rl_store_setting)
//    public void onStoreSettingClick() {
//        //TODO......................
//    }

    /**
     * 检查账户是否设置过银行卡，否则不能进入提现设置
     *
     * @return
     */
    private void checkBank() {
        loadingDialog.show();
        new HttpHandler(new HttpResponseListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                Bank bank = (Bank) response.getData();
                if (bank.getCardNo() == null || bank.getCardNo().equals("")) {
                    ToastUtils.showShort("请先绑定银行卡");
                } else {
                    intent2Activity(PaySettingActivity.class);
                }
            }

            @Override
            public void onFailed(BaseResponse response) {
//                if (response.getErrorCode() == ErrorCode.CODE_NO_BANK) {   //没有绑定银行卡的错误码
//                    ToastUtils.showShort(response.getMoreInfo());
//                }
            }

            @Override
            public void onFinish() {
                if (loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
            }
        }).getCeoUserIdBank();
    }

    /**
     * 开启 / 关闭 商家营业状态
     */
    @OnClick(R.id.sv_business_state)
    public void onBusinessStateClick() {
        SetBusinessStateDialog dialog = new SetBusinessStateDialog(getActivity(), svBusinessState);
        dialog.show();
    }

    /**
     * 开启 / 关闭 语音提示
     */
    @OnClick(R.id.sv_audio_button)
    public void onClick() {

        SharedPreferencesUtils.put(SharedPreferencesUtils.AUDIO_ATTENTION, svAudioButton.isOpened());

//        svAudioButton.setOpened(!svAudioButton.isOpened());
//        if (svAudioButton.isOpened()) {
//            ToastUtils.showShort("true");
//        } else {
//            ToastUtils.showShort("false");
//        }

//        SetAudioDialog dialog = new SetAudioDialog(getActivity(), svAudioButton);
//        dialog.show();
    }
}
